package com.yuwenxin.servlet.controller.user;

import com.yuwenxin.bean.User;
import com.yuwenxin.bean.queryModel.UserQueryModel;
import com.yuwenxin.core.PasswordEncodeUtil;
import com.yuwenxin.service.UserService;
import com.yuwenxin.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginController extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("LoginController initial...");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");

        UserQueryModel form = new UserQueryModel();
        form.setName(name);
        form.setPassword(password); // 这里加密了
        //这里是验证form的合法性 --9.20
        if (!form.login_validate(password)){
            req.setAttribute("form",form);
            req.getRequestDispatcher("/client_jsp/login.jsp").forward(req,resp);
            return;
        }
        System.out.println("登录验证...");
        User user;
        //这里开始是验证服务器端数据的合法性--9.20
        try {
            System.out.println("进入tyr代码块...");
            UserService service = new UserServiceImpl();
            user = service.login(form);
            System.out.println("调用service...");

            // 多条件判断，因此login函数不能简单的返回一个boolean值
            if (user == null){
                req.setAttribute("DBMes", "用户不存在");
                req.setAttribute("form", form);
                req.getRequestDispatcher("/client_jsp/login.jsp").forward(req,resp);
                return;  // 悲伤的故事还有那么多，讲述者的笑声掩饰着私下的啜泣。
            }
            if (!user.getPassword().equals(PasswordEncodeUtil.encodePassword(password))){
                req.setAttribute("PSWDMes","密码错误");
                req.setAttribute("form", form);
                req.getRequestDispatcher("/client_jsp/login.jsp").forward(req,resp);
                return;
            }else {
                service.userUpdate(user);
                req.getSession().setAttribute("user", user);
                req.getSession().setAttribute("username", user.getName());
//                System.out.println(user);
//                System.out.println("user已存入session中...");
//        添加自动登录所需要的Cookie

//                System.out.println("正在添加cookie...");
                RegisterController.autoLogin(req, resp, name, password);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }

        resp.getWriter().println("注册成功，3秒后跳转...");
        resp.setHeader("refresh","3;url=../index.jsp");

    }

    @Override
    public void destroy() {
        System.out.println("LoginController destroy...");
    }
}
